package android.slc.icr.core;

import android.slc.icr.core.entity.IcrChartRoom;
import android.slc.icr.core.entity.IcrMessage;
import android.slc.icr.core.entity.IcrUserInfo;
import android.slc.icr.core.room.manager.IChartRoomManager;
import android.slc.icr.service.IcrMessageRemoteService;
import android.slc.icr.service.IcrPersistenceService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class IcrManager implements IIcrManager {
    private static final IcrManager ICR_MANAGER = new IcrManager();
    private final Map<String, IChartRoomManager> roomManagerMap = new HashMap<>();
    private IcrMessageRemoteService icrMessageRemoteService;
    private IcrPersistenceService icrPersistenceService;

    /**
     * 获取其实例
     *
     * @return
     */
    public static IIcrManager getInstance() {
        return ICR_MANAGER;
    }

    private IcrManager() {

    }

    @Override
    public void init(IcrMessageRemoteService icrMessageRemoteService, IcrPersistenceService icrPersistenceService) {
        this.icrMessageRemoteService = icrMessageRemoteService;
        this.icrPersistenceService = icrPersistenceService;
    }

    @Override
    public void registerRoomManager(IChartRoomManager roomManager) {
        roomManager.setMustParam(this.icrMessageRemoteService, this.icrPersistenceService);
        roomManagerMap.put(roomManager.getRoomMsgType(), roomManager);
    }

    @Override
    public <T extends IChartRoomManager> T findRoomManager(String roomManagerType) {
        return (T) roomManagerMap.get(roomManagerType);
    }

    @Override
    public void receiveMessage(IcrMessage message) {
        if (message != null) {
            if (StringUtils.isNotEmpty(message.getRelate())) {
                IChartRoomManager iChartRoomManager = roomManagerMap.get(message.getRelate());
                if (iChartRoomManager != null) {
                    iChartRoomManager.receiveMessage(message);
                }
            } else if (StringUtils.isNotEmpty(message.getSender())) {
                //可能出现在用户掉线这样的场景
                CollectionUtil.forEach(icrPersistenceService.getIcrUserChartRoomService().findByUserId(message.getSender()),
                        new CollUtil.Consumer<IcrUserInfo>() {
                            @Override
                            public void accept(IcrUserInfo value, int index) {
                                if (StringUtils.isNotEmpty(value.getRelate())) {
                                    IcrMessage icrMessageTemp = IcrMessage.newBuilder().build();
                                    BeanUtil.copyProperties(message, icrMessageTemp, false);
                                    icrMessageTemp.setRelate(value.getRelate());
                                    icrMessageTemp.setRelateId(value.getRelateId());
                                    IChartRoomManager iChartRoomManager = roomManagerMap.get(icrMessageTemp.getRelate());
                                    if (iChartRoomManager != null) {
                                        iChartRoomManager.receiveMessage(icrMessageTemp);
                                    }
                                }
                            }
                        });
            }
        }
    }

    @Override
    public List<IcrChartRoom> getChartRoomList(String msgType) {
        IChartRoomManager roomManager = this.roomManagerMap.get(msgType);
        if (roomManager != null) {
            return roomManager.getOnlyReadChartRoomList();
        }
        return new ArrayList<>();
    }

    @Override
    public List<IcrChartRoom> getChartRoomListByCreateUser(String msgType, String createUser) {
        List<IcrChartRoom> icrChartRoomList = new ArrayList<>();
        if (StringUtils.isEmpty(msgType)) {
            CollectionUtil.forEach(roomManagerMap.values(), (CollUtil.Consumer<IChartRoomManager>) (roomManager, index) -> {
                if (roomManager != null) {
                    CollectionUtil.forEach(roomManager.getOnlyReadChartRoomList(), (CollUtil.Consumer<IcrChartRoom>) (value, index1) -> {
                        if (StringUtils.equals(value.getCreateUserId(), createUser)) {
                            icrChartRoomList.add(value);
                        }
                    });
                }
            });
        } else {
            IChartRoomManager roomManager = this.roomManagerMap.get(msgType);
            if (roomManager != null) {
                CollectionUtil.forEach(roomManager.getOnlyReadChartRoomList(), (CollUtil.Consumer<IcrChartRoom>) (value, index) -> {
                    if (StringUtils.equals(value.getCreateUserId(), createUser)) {
                        icrChartRoomList.add(value);
                    }
                });
            }
        }
        return icrChartRoomList;
    }

    @Override
    public IcrChartRoom findChartRoomByRoomId(String roomId, String msgType) {
        IChartRoomManager roomManager = this.roomManagerMap.get(msgType);
        if (roomManager != null) {
            return roomManager.findOnlyReadChartRoomByRoomId(roomId);
        }
        return null;
    }

}
